// Sprite.cpp //
#include "sprite.h"

//used to control how sprites are displayed, mainly for animation purposes
//although non animated objects also stored as sprites

Sprite::Sprite(string keyname)//constructor
{
    texture = Texture::TextureList[keyname];
 	origin = Vector(texture->tileWidth / 2, texture->tileHeight / 2);
	frame = 0; // needed for animation
	frameTimeRemaining = texture->frameInterval; // controls animation speed
	textureIndex = 0;
}

void Sprite::Update(float deltaTime)
{
	frameTimeRemaining -= deltaTime;
	if(frameTimeRemaining < 0) // changes animation frame
	{
		frame++;
		frame %= texture->frameCount;
		frameTimeRemaining = texture->frameInterval;
	}
}

// render either the plain or damaged texture
void Sprite::Render(SDL_Surface* screen, Vector position)
{
	SDL_Surface* surface = texture->textures[textureIndex];
	
	SDL_Rect location;
	location.x = (Sint16)(position.X - origin.X);
	location.y = (Sint16)(position.Y - origin.Y);

	SDL_Rect clip = GetFrameBounds();
	
    SDL_BlitSurface(surface, &clip, screen, &location);
}

// gets a rectangle around the current frame of animation
SDL_Rect Sprite::GetFrameBounds()
{
	SDL_Rect frameBounds;
	frameBounds.x = frame % texture->tilesX * texture->tileWidth;
	frameBounds.y = frame / texture->tilesX * texture->tileHeight;
	frameBounds.w = texture->tileWidth;
	frameBounds.h = texture->tileHeight;
	
	return frameBounds;
}

// switch to damage texture if it has one
void Sprite::SetTextureIndex(int index)
{
	if(texture->collidable)
	    textureIndex = index;
}
